# Eli Sowash
# IST554, Worksheet 8
# 02-27-2013

import sqlite3

class car(): 
    
    def __init__(self, **kwargs):
        self.variables = kwargs
        
    def set_variable(self, kwarg, var):
        self.variables[kwarg] = var
        
    def get_variable(self, kwarg):
        return(self.variables.get(kwarg))
           
def main():
        
    database = sqlite3.connect('insurance.sqlite')
    database.row_factory = sqlite3.Row
    
    #Load the DB into the namespace 'automobiles' because I'm getting confused by so many cars 
    automobiles = {}
    input = database.execute('select IDENT, MAKE, MODEL, DISPLACEMENT, POWER, LUXURY from CARS')
    print('A list of all the cars with their attributes:')
    for row in input:
       objectname = str(row['IDENT'])
       automobiles[objectname] = car(make = row['MAKE'], model = row['MODEL'], disp = row['DISPLACEMENT'], power = row['POWER'], luxury = row['LUXURY'], gas = 0, mpg = 20)
       print(row['IDENT'], row['MAKE'], row['MODEL'], row['DISPLACEMENT'], row['POWER'], row['LUXURY'])
       
    printseperator()
    
    #Call the functions
        
    mycar = 'fiPA12'  #because who *doesn't* want a Fiat Panda
    
    refuel(automobiles[mycar], 14)
    
    drive(automobiles[mycar], 10)
    
    range(automobiles[mycar])
         

def refuel(car, gallons):
    print('Adding {} gallons of fuel to the {} {}...'.format(gallons, car.get_variable('make'), car.get_variable('model')))
    fuel = car.get_variable('gas') + gallons
    car.set_variable('gas', fuel)
    print('The {} {} now has {} gallons of fuel.'.format(car.get_variable('make'), car.get_variable('model'), car.get_variable('gas')))
        
def drive(car, miles):
    consumed = miles / car.get_variable('mpg')
    fuel = car.get_variable('gas') - consumed
    car.set_variable('gas', fuel)
    print('You have driven the {} {} for {} miles, and used {} gallons of fuel'.format(car.get_variable('make'), car.get_variable('model'), miles, consumed))
    
def range(car):
    capability = car.get_variable('gas') * car.get_variable('mpg')
    print('You can now drive the {} {} for {} miles'.format(car.get_variable('make'), car.get_variable('model'), capability))

def printseperator():
    print('----------------------------------------------------------------------')




  
  


if __name__ == "__main__": main()
